package collectors.calendar.models;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Id;

import play.data.validation.Constraints.Email;
import play.data.validation.Constraints.Required;
import play.libs.F.Promise;
import play.libs.WS.Response;
import collectors.calendar.util.OAuthUtil;

@Entity
@DiscriminatorValue(value="DTYPE") 
public class GCalendarToken extends GoogleToken {
	
	@Required
	public String tokenType;

	@Required
	public String accessToken;
	
	//TODO worry about this later
	/*
	@Required
	public String refreshToken;
	*/
	@Required
	public long expiresIn;
	@Required
	public long grantedAt;

	@Override
	public String toString() {
		return "GCalendarToken [accessToken=" + accessToken + ", tokenType=" + tokenType + ", grantedAt="
				+ grantedAt + /*", refreshToken=" + refreshToken +*/
				", expiresIn=" + expiresIn + "]";
	}
	
	public static GCalendarToken create(String accessToken, String tokenType, long expires, long granted) {
		GCalendarToken tokenData = new GCalendarToken();
		tokenData.tokenType = tokenType;
		tokenData.accessToken = accessToken;
		tokenData.expiresIn = expires;
		tokenData.grantedAt = granted;
		
		//unidirectional relationship -> we save it on YouUser
//		data.save();
		return tokenData;
	}

	@Override
	public Promise<Response> unlink() {
		return OAuthUtil.revokeToken(accessToken);
	}
}
